package flirtme.libraries.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.Base64;
import android.util.Log;

public class Utils {
    
	public static void CopyStream(InputStream is, OutputStream os)
    {
        final int buffer_size=1024;
        try
        {
            byte[] bytes=new byte[buffer_size];
            for(;;)
            {
              int count=is.read(bytes, 0, buffer_size);
              if(count==-1)
                  break;
              os.write(bytes, 0, count);
            }
        }
        catch(Exception ex){}
    }
    
	public static Boolean runningOnSimulator() {
		return Build.BRAND.equalsIgnoreCase("generic");
	}
	
	 public static String encodeTobase64(Bitmap image)
	 {
	     Bitmap inputImage = image;
	     ByteArrayOutputStream outputStream = new ByteArrayOutputStream();  
	     inputImage.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
	     byte[] bytes = outputStream.toByteArray();
	     String imageEncoded = Base64.encodeToString(bytes,Base64.DEFAULT);

	     Log.d("Image Encoded:", imageEncoded);
	     return imageEncoded;
	 }
	 
	 public static Bitmap decodeBase64(String input) 
	 {
	     byte[] decodedByte = Base64.decode(input, 0);
	     return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length); 
	 }
	 
	 public static Bitmap createBitmapFromUrl( String rawUrl)
	 {
		 URL url = null;
		try {
			url = new URL(rawUrl);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 Bitmap image = null;
		try {
			image = BitmapFactory.decodeStream(fetchInputStream(rawUrl));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 return image;
	 }
	 
	 private static InputStream fetchInputStream(String address) throws MalformedURLException,IOException {
		    HttpGet httpRequest = new HttpGet(URI.create(address) );
		    HttpClient httpclient = new DefaultHttpClient();
		    HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
		    HttpEntity entity = response.getEntity();
		    BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
		    InputStream instream = bufHttpEntity.getContent();
		    return instream;
	 }
	 
	 public static Drawable createDrawableFromWebUrl(String rawUrl) {

		        InputStream is = null;
		        URL url = null;
				try {
					url = new URL(rawUrl);
					is = (InputStream) url.getContent();
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		        Drawable d = Drawable.createFromStream(is, "src name");
		        return d;

		}
	 
	 
    
}